package asteroids.engine;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyboardMonitor implements KeyListener
{
    static KeyboardMonitor monitor;

    private KeyboardMonitor()
    {
    }

    public static KeyboardMonitor getInstance()
    {
        if( monitor == null )
        {
            monitor = new KeyboardMonitor();
        }
        return monitor;
    }

    private static boolean[] keyStates = new boolean[100];

    @Override
    public void keyPressed(KeyEvent key)
    {
        if( key.getKeyCode() == KeyEvent.VK_ESCAPE )
        {
            System.exit(0);
        }
        else
        {
            if( key.getKeyCode() < 101 )
            {
                keyStates[key.getKeyCode()] = true;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent key)
    {
        if( key.getKeyCode() < 101 )
        {
            keyStates[key.getKeyCode()] = false;
        }
    }

    @Override
    public void keyTyped(KeyEvent key)
    {
        // bah, we care not about typing.
    }

    public static boolean getKeyDown(int keyCode)
    {
        return keyStates[keyCode];
    }
}
